<?php 
class CategoryController extends Controller{
	private $db;
	private $cid;
	//构造函数
	public function __init(){
		$this->db=K('Category');
		$this->cid=Q('$cid',0,'intval');
		if ($this->cid and !$this->db->find($this->cid)) {
			$this->error('栏目不存在','index');
		}
	}
	//显示页面信息
	public function index(){
		$data = $this->db->all();
		$data = Data::tree($data,'catname');
		$this->assign('data',$data);
		$this->display();
	}
	//添加栏目信息
	public function add(){
		if (IS_POST) {
			if ($this->db->addCategory()) {
				$this->success('添加成功','index');
			}else{
				$this->error($this->db->error);
			}
		}else{
			$goods_type=M('goods_type')->all();
			$category=$this->db->all();
			$this->assign('category',Data::tree($category,'catname'));
			$this->assign('goods_type',$goods_type);
			$this->display();
		}
	}
	//编辑栏目
	public function edit(){
		if (IS_POST) {
			if ($this->db->editCategory()) {
				$this->success('编辑成功','index');
			}else{
				$this->error($this->db->error);
			}
		}else{
			$cid=Q('cid',0,'intval');
			$field = $this->db->find($cid);
			$goods_type = M('goods_type')->all();
			$category = $this->db->all();
			foreach ($category as $id => $cat) {
				if(Data::isChild($category,$cat['cid'],$field['cid'])){
					$category[$id]['disabled'] = 'disabled=""';
				}
			}
			$this->assign('field',$field);
			$this->assign('category',Data::tree($category,'catname'));
			$this->assign('goods_type',$goods_type);
			$this->display();
		}
	}

	//删除栏目
	public function del(){
		$this->display();
	}
}


 ?>